package com.ruoyi.project.mtool.controller;

import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.project.mtool.domain.Product;
import com.ruoyi.project.mtool.service.IProductService;
import com.ruoyi.project.tool.gen.domain.GenTable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.mtool.domain.CustomerProduct;
import com.ruoyi.project.mtool.service.ICustomerProductService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 客户产品关联Controller
 *
 * @author ruoyi
 * @date 2020-04-08
 */
@RestController
@RequestMapping("/mtool/customerProduct")
public class CustomerProductController extends BaseController {
    @Autowired
    private ICustomerProductService customerProductService;
    @Autowired
    private IProductService productService;

    /**
     * 查询客户产品关联列表
     */
    @PreAuthorize("@ss.hasPermi('mtool:customerProduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerProduct customerProduct) {
        startPage();
        List<CustomerProduct> list = customerProductService.selectCustomerProductList(customerProduct);
        return getDataTable(list);
    }

    /**
     * 导出客户产品关联列表
     */
    @PreAuthorize("@ss.hasPermi('mtool:customerProduct:export')")
    @Log(title = "客户产品关联", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CustomerProduct customerProduct) {
        List<CustomerProduct> list = customerProductService.selectCustomerProductList(customerProduct);
        ExcelUtil<CustomerProduct> util = new ExcelUtil<CustomerProduct>(CustomerProduct.class);
        return util.exportExcel(list, "customerProduct");
    }

    /**
     * 获取客户产品关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('mtool:customerProduct:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(customerProductService.selectCustomerProductById(id));
    }

    /**
     * 新增客户产品关联
     */
    @PreAuthorize("@ss.hasPermi('mtool:customerProduct:add')")
    @Log(title = "客户产品关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerProduct customerProduct) {
        return toAjax(customerProductService.insertCustomerProduct(customerProduct));
    }

    /**
     * 修改客户产品关联
     */
    @PreAuthorize("@ss.hasPermi('mtool:customerProduct:edit')")
    @Log(title = "客户产品关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerProduct customerProduct) {
        return toAjax(customerProductService.updateCustomerProduct(customerProduct));
    }

    /**
     * 删除客户产品关联
     */
    @PreAuthorize("@ss.hasPermi('mtool:customerProduct:remove')")
    @Log(title = "客户产品关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(customerProductService.deleteCustomerProductByIds(ids));
    }

    /**
     * 导入产品信息到关联表（保存）
     */
    @PreAuthorize("@ss.hasPermi('tool:gen:list')")
    @Log(title = "导入产品与客户关联", businessType = BusinessType.IMPORT)
    @PostMapping("/importProduct")
    public AjaxResult importTableSave(String idStr,Long customerId) {
        Long[] ids = Convert.toLongArray(idStr);
        List<Product> list = productService.selectProductByIds(ids);
        customerProductService.importCustomerProduct(list,customerId);
        return AjaxResult.success();
    }
}
